<p>Test sending and receiving small messages of different lengths.</p>
<p>Should say PASS:</p>
<pre id=log>Running the test...
</pre>
<script>
var maxLength = 1025;

if (window.testRunner) {
    testRunner.dumpAsText();
    testRunner.waitUntilDone();
}

function log(message)
{
    document.getElementById("log").innerHTML += message + "\n";
}

var ws = new WebSocket("ws://127.0.0.1:8880/echo");

var lastString;
var failed = false;
ws.onopen = function() {
    lastString = "";
    ws.send(lastString);
}

ws.onmessage = function(msg) {
    if (msg.data != lastString) {
        log("FAIL - incorrect response. Expected '" + lastString + "', got '" + msg.data + "'.");
        failed = true;
    }

    if (lastString.length == maxLength) {
        if (!failed)
            log("PASS");
        ws.send("Goodbye");
        ws.onmessage = null;
        if (window.testRunner)
            setTimeout("testRunner.notifyDone()", 0);
    } else {
        lastString += "*";
        ws.send(lastString);
    }
}
</script>
